﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using System.Drawing;
using GamePlugin.Logics.Data;
using WindowsInput.Native;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRunProperty : MachineState
    {
        public static int HelperPropertyIndex = 0;

        public int PropertyIndex
        {
            get;
            set;
        }

        public DActionRunProperty(int propertyIndex)
        {
            this.PropertyIndex = propertyIndex;
        }

        public override void Initialize()
        {
            this.PropertyIndex = HelperPropertyIndex;

            this.AddState("PropertyRemove");
            this["PropertyRemove"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.PropertyRemove(o, e));

            this.AddState("PropertySelect");
            this["PropertySelect"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.PropertySelect(o, e));

            this.AddState("PropertyInput");
            this["PropertyInput"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.PropertyInput(o, e));

            this.AddState("Finished");
            this["Finished"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.Finished(o, e));

            this.DefaultState = "PropertyRemove";
        }

        public void PropertyRemove(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            // reset all
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            if (this.PropertyIndex == 0)
            {
                for (int propertyI = 0; propertyI < ActionData.PropertyCount; propertyI++)
                {
                    Workspace.Instance.SearchItemCompare.PropertyFilled[propertyI] = true;
                    if (Workspace.Instance.SearchItemCompare.ItemPropertyChanged[propertyI] == false)
                        continue;

                    Workspace.Instance.SearchItemCompare.PropertyFilled[propertyI] = false;
                    // move mouse and open list
                    Helper.MoveCursor(ActionData.RemovePropertyPoint[propertyI]);
                    Helper.Sleep(100);
                    input.Mouse.LeftButtonClick();
                    Helper.Sleep(100);
                }
            }

            if (Workspace.Instance.SearchItemCompare.ItemPropertyChanged[this.PropertyIndex] == false)
                this.ChangeToState("Finished");
            else
                this.ChangeToState("PropertySelect");
        }

        public void PropertySelect(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            // reset all
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            // select choice
            int allCount = Helper.GetFilterPropertyCount(Workspace.Instance.SearchItem, this.PropertyIndex);
            int selectedItem = Helper.GetFliterPropertyIndex(Workspace.Instance.SearchItem, this.PropertyIndex);
            if (selectedItem == 0)
            {
                this.ChangeToState("Finished");
                return;
            }

            this.SelectItem(ActionData.SelectPropertyRectangle[this.PropertyIndex], selectedItem, allCount);

            if (Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.TestAllWithLowValuesMode)
                this.ChangeToState("Finished");
            else
                this.ChangeToState("PropertyInput");
        }

        private void SelectItem(Rectangle rect, int selectedItem, int allCount)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            if (allCount < ActionData.OnePageCount)
                allCount = ActionData.OnePageCount;

            int currentStartIndex = 0;

            //while (selectedItem > onePageHeight
            while (selectedItem > currentStartIndex + ActionData.OnePageCount - 1)
            {
                // move mouse and open list
                Helper.MoveCursor(ActionData.SelectPropertyPoint[this.PropertyIndex]);
                Helper.Sleep(100);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(100);

                int posY = (int)(((rect.Bottom - rect.Top) / ActionData.OnePageCount) * (ActionData.OnePageCount - 1 + 0.5f));
                Helper.MoveCursor(ActionData.PropertySelectX, posY + rect.Top);
                Helper.Sleep(100);
                input.Mouse.LeftButtonClick();

                currentStartIndex += ActionData.OnePageCount - 1;
            }

            if (selectedItem <= currentStartIndex + ActionData.OnePageCount - 1 &&
                currentStartIndex + ActionData.OnePageCount - 1 <= allCount - 1)
                selectedItem = selectedItem - currentStartIndex;
            else
                selectedItem = ActionData.OnePageCount - (allCount - selectedItem);

            {
                // move mouse and open list
                Helper.MoveCursor(ActionData.SelectPropertyPoint[this.PropertyIndex]);
                Helper.Sleep(100);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(100);

                int posY = (int)(((rect.Bottom - rect.Top) / ActionData.OnePageCount) * (selectedItem + 0.5f));
                Helper.MoveCursor(ActionData.PropertySelectX, posY + rect.Top);
                Helper.Sleep(100);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(100);
            }
        }

        public void PropertyInput(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            Helper.MoveCursor(ActionData.SelectPropertyValuePoint[this.PropertyIndex]);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            // input value
            var number = Helper.GetPropertyValue(Workspace.Instance.SearchItem, this.PropertyIndex);
            if (number > 0)
            {
                System.Windows.Forms.Clipboard.Clear();
                System.Windows.Forms.Clipboard.SetText(number.ToString());
                Helper.Sleep(111);

                input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
                Helper.Sleep(111);
            }

            this.ChangeToState("Finished");
        }

        public void Finished(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            Workspace.Instance.SearchItemCompare.PropertyFilled[this.PropertyIndex] = true;
            this.IsNeedReset = true;
            if (this.PropertyIndex == ActionData.PropertyCount - 1)
            {
                this.Owner.ChangeToState(typeof(DActionRunSearchBuyAution).Name);
                return;
            }
            else
                this.Owner.ChangeToState(Helper.GetPropertyStateName(this.PropertyIndex + 1));
        }
    }
}
